package com.atguigu.lease.web.admin.config;

import com.atguigu.lease.common.converter.StringToItemTypeConverter;
import com.atguigu.lease.common.converter.StringToItemtypeConverterFactory;
import com.atguigu.lease.interceptor.AuthenticationInterceptor;
import com.atguigu.lease.properties.AutoPathProperties;
import org.aopalliance.intercept.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootConfiguration
@EnableConfigurationProperties(value = AutoPathProperties.class)
public class WebAdminMvcConfig implements WebMvcConfigurer {

    @Autowired
//    private StringToItemTypeConverter stringToItemTypeConverter;
    private StringToItemtypeConverterFactory stringToItemtypeConverterFactory;

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;
    @Autowired
    private AutoPathProperties autoPathProperties;

    @Override
    public void addFormatters(FormatterRegistry registry) {

//        registry.addConverter(stringToItemTypeConverter);
        registry.addConverterFactory(stringToItemtypeConverterFactory);

    }

    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(authenticationInterceptor);//现在是所有都拦截了，但还要排除两个

        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns(autoPathProperties.getInclude())  //拦截路径
                .excludePathPatterns(autoPathProperties.getExclude());  //排除路径
    }


}
